/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.ConnectionFactory;
import model.beans.TipoUsuario;

/**
 *
 * @author Carol
 */
public class TipoUsuarioDAO {
 private Connection con;
    private PreparedStatement stmtListar;
    private PreparedStatement stmtBuscarPorId;
    
    public TipoUsuarioDAO() throws Exception{
        con = ConnectionFactory.getConnection();
    }
    
    public TipoUsuarioDAO(Connection con) throws Exception{
        this.con = con;        
    }
    
    public List<TipoUsuario> listar() throws Exception{
        stmtListar = con.prepareStatement("SELECT idTipoUsuario, tipoUsuario FROM tipousuario");
        ResultSet rs = stmtListar.executeQuery();
        List<TipoUsuario> lista = new ArrayList();
        while(rs.next()){
            TipoUsuario tu = new TipoUsuario();
            tu.setId(rs.getInt("idTipoUsuario"));
            tu.setNome(rs.getString("tipoUsuario"));
            lista.add(tu);
        }
        stmtListar.close();
        return lista;
    }
    
    public TipoUsuario buscarPorId(int id) throws SQLException{
        stmtBuscarPorId = con.prepareStatement("SELECT tipoUsuario FROM tipousuario WHERE idTipoUsuario = ? ");
        TipoUsuario tipo = new TipoUsuario();
        
        stmtBuscarPorId.setInt(1, id);
        ResultSet rs = stmtBuscarPorId.executeQuery();
        
        if(rs.next()){
            tipo.setId(id);
            tipo.setNome(rs.getString("tipoUsuario"));
        }else{
            tipo.setId(0);
        }
        
        return tipo;
    }
}